#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll n;
ll a[200001], b[200001];
bool v[400001];
map<pair<ll,ll>,int>mp;
vector<ll>f[400001];
vector<ll>e[400001];
ll k1,k2;
bool g[400001];
void dfs(ll x) {
	g[x]=1;
	k2+=e[x].size();
	k1++;
	for(unsigned int j=0;j<e[x].size();j++)
		if(!g[e[x][j]])
			dfs(e[x][j]);
}
ll sum=0;
void dfs2(ll x) {
	if(v[x]==0)
		sum++;
	v[x]=1;
	for(unsigned int j=0;j<f[x].size();j++)
		if(v[f[x][j]]==0)
			dfs2(f[x][j]);
}
int main() {
	freopen("pairint.in","r",stdin);
	freopen("pairint.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i] >> b[i];
		if(a[i]>b[i])
			swap(a[i],b[i]);
		if(a[i]==b[i]&&v[a[i]]==0)
			sum++,v[a[i]]=1;
		mp[make_pair(a[i],b[i])]++;
		f[a[i]].push_back(b[i]);
		f[b[i]].push_back(a[i]);
	}
	for(int i=1;i<=n;i++) 
		if(mp[make_pair(a[i],b[i])]>1)
			sum+=(v[a[i]]==0)+(v[b[i]]==0),v[a[i]]=1,v[b[i]]=1;
	for(int i=1;i<=2*n;i++)
		if(v[i])
			dfs2(i);
	for(int i=1;i<=n;i++) {
		if(mp[make_pair(a[i],b[i])]==1&&a[i]!=b[i]) {
			if(v[a[i]]==0&&v[b[i]]==0) {
				e[a[i]].push_back(b[i]);
				e[b[i]].push_back(a[i]);
			} 
		}
	}
	for(int i=1;i<=2*n;i++) 
		if(e[i].size()&&g[i]==0) {
			k1=0,k2=0;
			dfs(i);
			sum+=min(k1,k2/2);
		}
	cout<<sum;
	return 0;
}
